import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import { dialogSelect } from "@/views/process-manage/src/components/person-selection/index";
import { State } from "vuex-class";
@Component
export default class ProcessNodeMixin extends Vue {
    @State(state => state.authArea) authArea: any;
    @Prop({ default: false }) private readOnly!: boolean
    @Prop({ default: {} }) private nodeInfo: any
    // 当前选中人
    selectedList = []

    activeName = "basis"
    updateForm: any = {
        name: ""
    }

    btnDisabled = false

    @Watch("nodeInfo", { immediate: true })
    changeInfo() {
        const { name, selectedList } = this.nodeInfo;
        this.updateForm = { name };
        this.selectedList = selectedList;
    }

    // 去修改节点内容
    toUpdateNode(val: any, defaultValue: any) {
        if (val) {
            /* if (!val.includes(defaultValue + "-") && val !== defaultValue) {
                val = defaultValue + "-" + val;
            } */
            if (val.match(/^[ ]*$/)) {
                val = defaultValue;
            }
        } else {
            val = defaultValue;
        }
        this.$emit("updateNode", { type: "handle", data: { name: val } });
    }

    // 选择人员
    async chooseManager() {
        this.btnDisabled = true;
        const selectedItemList = await this.getUserList();
        dialogSelect({
            selectedList: this.selectedList,
            selectedItemList: selectedItemList,
            isSingle: true,
            tabs: [{
                label: "镇成员",
                name: "town"
            }, {
                label: "村成员",
                name: "village",
                type: "group"
            }],
            readOnly: this.readOnly,
            sure: (vm: any, model: any) => {
                vm.close();
                this.selectedList = model.selectedList;
                this.$emit("updateNode", { type: "handle", data: { "activiti:assignee": this.selectedList[0] } });
            },
            getOptions: (vm: any, model: any) => this.getOptions(vm, model),
            close: () => {
                this.btnDisabled = false;
            }
        });
    }

    // 获取选项
    async getOptions(vm: any, model: any) {
        if (model.name === "town") {
            const res = await this.$server["org/getMember"]({
                data: {
                    departmentId: "",
                    pageSize: 999,
                    currentPage: 1
                },
                loader: false
            });
            if (res.data.code === 200) {
                vm.getOptions(res.data.data.records || []);
            } else {
                vm.getOptions([]);
            }
        } else if (model.name === "village") {
            const res = await this.$server["process/getGovernmentUser"]({
                data: {
                    governmentId: this.authArea.governmentId
                },
                loader: false
            });
            if (res.data.code === 200) {
                vm.getOptions(res.data.data || []);
            } else {
                vm.getOptions([]);
            }
        }
    }

    // 根据id获取用户信息
    async getUserList() {
        if (!this.selectedList.length) return [];
        const res = await this.$server["org/getUserInfoByIds"]({
            data: this.selectedList
        });
        if (res.data.code === 200) {
            return res.data.data;
        } else {
            return [];
        }
    }
}
